package com.fy.customview.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fy.customview.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewTestActivity extends AppCompatActivity {

    private RecyclerView rcv_recycler_ver_test;
    private RecyclerView.Adapter<Holder1> adapter;

    private List<Data1> data1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_test);
        data1 = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Data1 data11 = new Data1();
            data11.text = "RecyclerView" + i;
            data11.data = new ArrayList<>();
            for (int j = 0; j < 20; j++) {
                data11.data.add("RecyclerView_" + i + "_" + j);
            }
            data1.add(data11);
        }
        rcv_recycler_ver_test = findViewById(R.id.rcv_recycler_ver_test);
        adapter = new RecyclerView.Adapter<Holder1>() {
            @NonNull
            @Override
            public Holder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new Holder1(LayoutInflater.from(RecyclerViewTestActivity.this).inflate(R.layout.item_recycler_view_test_1, parent, false));
            }

            @Override
            public void onBindViewHolder(@NonNull Holder1 holder, final int position) {
                if (holder.rcv_item_1.getAdapter() == null) {
                    holder.adapter2 = new RecyclerView.Adapter<Holder2>() {
                        @NonNull
                        @Override
                        public Holder2 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                            return new Holder2(LayoutInflater.from(RecyclerViewTestActivity.this).inflate(R.layout.item_recycler_view_test_2, parent, false));
                        }

                        @Override
                        public void onBindViewHolder(@NonNull Holder2 holder, int position) {
                            holder.tv_item_recycler_text_1.setText("onBindViewHolder");
                        }

                        @Override
                        public int getItemCount() {
                            return data1.get(position).data.size();
                        }
                    };

                    LinearLayoutManager layoutManager2 = new LinearLayoutManager(RecyclerViewTestActivity.this);
                    layoutManager2.setOrientation(RecyclerView.HORIZONTAL);
                    holder.rcv_item_1.setAdapter(holder.adapter2);
                    holder.rcv_item_1.setLayoutManager(layoutManager2);
                }
                holder.tv_item_recycler_text_1.setText(data1.get(position).text);
            }

            @Override
            public int getItemCount() {
                return data1.size();
            }
        };

        rcv_recycler_ver_test.setAdapter(adapter);
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(RecyclerView.VERTICAL);
        rcv_recycler_ver_test.setLayoutManager(layoutManager1);
    }

    private class Adapter1 extends RecyclerView.Adapter<Holder1>{
        private List<String> data;
        @NonNull
        @Override
        public Holder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new Holder1(LayoutInflater.from(RecyclerViewTestActivity.this).inflate(R.layout.item_recycler_view_test_1, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder1 holder, int position) {

        }

        @Override
        public int getItemCount() {
            return data!=null?data.size():0;
        }
    }

    private static class Holder1 extends RecyclerView.ViewHolder {
        private RecyclerView rcv_item_1;
        private RecyclerView.Adapter<Holder2> adapter2;
        private TextView tv_item_recycler_text_1;

        private Holder1(@NonNull View itemView) {
            super(itemView);
            rcv_item_1 = itemView.findViewById(R.id.rcv_item_1);
            tv_item_recycler_text_1 = itemView.findViewById(R.id.tv_item_recycler_text_1);
        }
    }

    private static class Holder2 extends RecyclerView.ViewHolder {
        private TextView tv_item_recycler_text_1;
        private Holder2(@NonNull View itemView) {
            super(itemView);
            tv_item_recycler_text_1 = itemView.findViewById(R.id.tv_item_recycler_text_1);
        }
    }

    private static class Data1 {
        private String text;
        private List<String> data;
    }

}
